package com.tgu.home_action.service.impl.admin;

import com.alibaba.fastjson.JSONArray;
import com.tgu.home_action.dao.CaseMapper;
import com.tgu.home_action.dao.FamilyMapper;
import com.tgu.home_action.dao.OldManMapper;
import com.tgu.home_action.pojo.*;
import com.tgu.home_action.pojo.state.ResponseState;
import com.tgu.home_action.service.OldManService;
import com.tgu.home_action.service.admin.AdminFamilyService;
import com.tgu.home_action.service.impl.OldManServiceImpl;
import com.tgu.home_action.service.utils.AliYunService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotNull;
import java.util.List;

@Validated
@Service
public class AdminFamilyServiceImpl implements AdminFamilyService {
    @Autowired
    private FamilyMapper familyMapper;
    @Autowired
    private OldManMapper oldManMapper;
    @Autowired
    private CaseMapper caseMapper;
    @Autowired
    private AliYunService aliYunService;

    /**
     * 获取家属用户
     */
    @Override
    public ResponseData<Page<Family>> getFamily(Family family) {
        int sum = familyMapper.getAccordFamilyNum(family);
        if (family.getPageNo() == null || family.getPageSize() == null) {
            family.setPageNo(1);
            family.setPageSize(sum);
        }
        family.setStartSize((family.getPageNo() - 1) * family.getPageSize());
        List<Family> families = familyMapper.adminGetFamily(family);
        int pageNum;
        if (family.getPageSize() == 0) {
            pageNum = 0;
        } else {
            pageNum = sum / family.getPageSize();
            if (sum % family.getPageSize() > 0) {
                pageNum++;
            }
        }
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), new Page<>(family.getPageNo(), family.getPageSize(), pageNum, sum, families));
    }

    /**
     * 修改家属信息
     */
    @Override
    public ResponseData<Object> updateFamilyInfo(Family family) {
        if (family.getState() != null && family.getState() != 0 && family.getState() != 1 && family.getState() != 2) {
            return new ResponseData<>("state只能是1或2", ResponseState.PARAM_IS_ERROR.getValue());
        }
        familyMapper.adminUpdateFamily(family);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 修改家属Id
     */
    @Override
    public ResponseData<Object> updateFamilyAvatar(@NotNull(message = "familyId不能为空") Integer familyId, @NotNull(message = "avatarUrl不能为空") String avatarUrl) {
        familyMapper.updateAvatar(familyId, avatarUrl);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 查询老人信息
     */
    @Override
    public ResponseData<List<OldMan>> getOldMen(Integer familyId) {
        List<OldMan> oldMEN = oldManMapper.getOldMenByFamilyId(familyId);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), oldMEN);
    }

    /**
     * 修改老人信息
     */
    @Override
    public ResponseData<Object> updateOldMan(OldMan oldMan) {
        Boolean isSuccess = oldManMapper.adminUpdateOldMan(oldMan);
        if (isSuccess) {
            try {
                aliYunService.deleteFaceEntity(OldManServiceImpl.entityPrefix + oldMan.getId());
                aliYunService.addFaceEntity(OldManServiceImpl.entityPrefix + oldMan.getId());
                aliYunService.addFace(oldMan.getIdentificationPhoto(), OldManServiceImpl.entityPrefix + oldMan.getId());
                System.out.println(JSONArray.toJSON(oldMan.getLifePhoto()));
                JSONArray lifePhotoJSONArray = JSONArray.parseArray(oldMan.getLifePhoto());
                Object[] lifePhotos = lifePhotoJSONArray.toArray();
                for (int i = 0; i < 4 && i < lifePhotos.length; i++) {
                    aliYunService.addFace((String) lifePhotos[i], OldManServiceImpl.entityPrefix + oldMan.getId());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return isSuccess ? new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue())
                : new ResponseData<>(ResponseState.OLD_MAN_NOT_EXIST.getMessage(), ResponseState.OLD_MAN_NOT_EXIST.getValue());
    }

    /**
     * 查询家属相关案件
     */
    @Override
    public ResponseData<Page<Case>> getCases(Case nowCase) {
        int sum = caseMapper.getFamilyAccordNumber(nowCase);
        if (nowCase.getPageNo() == null || nowCase.getPageSize() == null) {
            nowCase.setPageNo(1);
            nowCase.setPageSize(sum);
        }
        nowCase.setStartSize((nowCase.getPageNo() - 1) * nowCase.getPageSize());
        List<Case> cases = caseMapper.getFamilyAccordCase(nowCase);
        int pageNum;
        if (nowCase.getPageSize() == 0) {
            pageNum = 0;
        } else {
            pageNum = sum / nowCase.getPageSize();
            if (sum % nowCase.getPageSize() > 0) {
                pageNum++;
            }
        }
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), new Page<>(nowCase.getPageNo(), nowCase.getPageSize(), pageNum, sum, cases));

    }

    /**
     * 查询老人相关的案件
     */
    @Override
    public ResponseData<Page<Case>> getOldManCases(Case nowCase) {
        int sum = caseMapper.getOldManAccordNumber(nowCase);
        if (nowCase.getPageNo() == null || nowCase.getPageSize() == null) {
            nowCase.setPageNo(1);
            nowCase.setPageSize(sum);
        }
        nowCase.setStartSize((nowCase.getPageNo() - 1) * nowCase.getPageSize());
        List<Case> cases = caseMapper.getOldManAccordCase(nowCase);
        int pageNum;
        if (nowCase.getPageSize() == 0) {
            pageNum = 0;
        } else {
            pageNum = sum / nowCase.getPageSize();
            if (sum % nowCase.getPageSize() > 0) {
                pageNum++;
            }
        }
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), new Page<>(nowCase.getPageNo(), nowCase.getPageSize(), pageNum, sum, cases));
    }

    /**
     * 删除家属用户
     */
    @Transactional
    @Override
    public ResponseData<Object> delFamily(Integer familyId) {
        Family family = familyMapper.getById(familyId);
        if (family == null) {
            return new ResponseData<>(ResponseState.USER_NOT_EXIST.getMessage(), ResponseState.USER_NOT_EXIST.getValue());
        }
        //取消案件绑定
        caseMapper.updateFamilyId(family.getId());
        //删除老人信息
        oldManMapper.deleteOldManByFamilyId(family.getId());
        //删除用户
        familyMapper.deleteById(family.getId());
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 删除老人信息
     */
    @Transactional
    @Override
    public ResponseData<Object> delOldMan(Integer oldManId) {
        OldMan oldMan = oldManMapper.getById(oldManId);
        if (oldMan == null) {
            return new ResponseData<>(ResponseState.OLD_MAN_NOT_EXIST.getMessage(), ResponseState.OLD_MAN_NOT_EXIST.getValue());
        }
        //取消案件绑定
        caseMapper.updateOldManId(oldMan.getId());
        Boolean isSuccess = oldManMapper.deleteOldMan(oldMan);
        if (isSuccess) {
            try {
                aliYunService.deleteFaceEntity(OldManServiceImpl.entityPrefix + oldManId);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
        } else {
            return new ResponseData<>(ResponseState.OLD_MAN_NOT_EXIST.getMessage(), ResponseState.OLD_MAN_NOT_EXIST.getValue());
        }
    }
}
